package com.itheima.d6_stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 掌握Stream流中的终结方法
 */
public class StreamDemo3 {
    public static void main(String[] args) {
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(new Teacher("卢宇浩", 17, 5000000));
        teachers.add(new Teacher("黄越", 22, 60000));
        teachers.add(new Teacher("元建敏", 30, 70000));
        teachers.add(new Teacher("汪浩", 20, 30000));
        teachers.add(new Teacher("陈炜炜", 31, 85000));
        teachers.add(new Teacher("陈晓杰", 22, 65000));
        teachers.add(new Teacher("赵方庆", 22, 60000));
        //终结方法1:forEach() 工资大于50000的打印
        teachers.stream().filter(t ->t.getSalary()>= 50000).forEach(System.out::println);
        System.out.println("---------------------------");


        //终结方法2:count()
        long count = teachers.stream().filter(t -> t.getSalary() >= 50000).count();
        System.out.println( count);
        System.out.println("---------------------------");

        //终结方法3:max()  获取薪水最高的老师对象
        Optional<Teacher> max = teachers.stream().max((t1, t2) -> Double.compare(t1.getSalary(), t2.getSalary()));
        System.out.println(max.get());
        System.out.println("---------------------------");

        //终结方法4:min()
        Optional<Teacher> min = teachers.stream().min((t1, t2) -> Double.compare(t1.getSalary(), t2.getSalary()));
        System.out.println(min.get());
        System.out.println("---------------------------");

        List<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        list.add("张三丰");
        //流只能收集一次
        Stream<String> s1 = list.stream().filter(s -> s.startsWith("张"));
        List<String> collect1 = s1.collect(Collectors.toList());
        System.out.println(collect1);
        //流只能收集1次 不能收集多次
//        List<String> list2 = s1.collect(Collectors.toList());
        //但是每次stream()都是新创建一个流
        list.stream().filter(s -> s.startsWith("张")).collect(Collectors.toList());
        list.stream().filter(s -> s.startsWith("张")).collect(Collectors.toList());
        System.out.println("---------------------------");

        //收集到Set集合中(无序，无索引，不可重复)(去重)
        Set<String> collect2 = list.stream().collect(Collectors.toSet());
        System.out.println(collect2);
        System.out.println("---------------------------");

        //收集到数组中
        Object[] array = list.stream().filter(s -> s.startsWith("张")).toArray();
        System.out.println(Arrays.toString(array));
        System.out.println("---------------------------");

        //收集到map集合中
        Map<String, Double> map1 = teachers.stream().collect(Collectors.toMap(Teacher::getName, Teacher::getSalary));
        System.out.println(map1);


    }
}
